let express = require('express')
let sql = require('./db')
const router = express.Router();
router.get('/read', function (req, res) {
    sql('select * from student order by id desc', null, function (err, data) {
        if (err != null) {
            res.send({
                status: 0,
                message: '查询失败'
            })
        } else {
            res.send({
                status: 1,
                message: '查询成功',
                data: data
            })
        }
    })
})
router.get('/delete', function (req, res) {
    let id = req.query
    sql('delete from student where?', id, function (err, data) {
        if (err != null) {
            res.send({
                status: 0,
                message: '删除失败'
            })
        } else {
            res.send({
                status: 1,
                message: '删除成功',
            })
        }
    })
})
router.post('/add', function (req, res) {
    console.log(req.body);
    sql('insert into student set? ', req.body, function (err, data) {
        if (err != null) {
            res.send({
                status: 0,
                message: '新增失败'
            })
        } else {
            res.send({
                status: 1,
                message: '新增成功',
            })
        }
    })
})
router.post('/update', function (req, res) {
    console.log(req.body);
    sql('update student set ? where id=?', [req.body, req.body.id], function (err, data) {
        if (err != null) {
            res.send({
                status: 0,
                message: '新增失败'
            })
        } else {
            res.send({
                status: 1,
                message: '新增成功',
            })
        }
    })
})

module.exports = router;